/*
 * @(#)    StringUtils.java
 * Description:
 *
 * Copyright (c) 2008 BeiJing UFC Co., Ltd
 * All right reserved.
 *
 * Create Date:     2009-3-3 下午01:37:15
 *
 */
package com.gaoxk.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

/**
 * 字符串工具类
 *
 * @author zoucx
 */
public final class StringUtil {
    private StringUtil() throws IllegalAccessException {
        throw new IllegalAccessException("Unable to instantiate StringUtil");
    }

    /**
     * 判断string为空或空串.
     *
     * @param string 字符串
     * @return
     */
    public static boolean isNullOrEmpty(final String string) {
        return string == null || string.isEmpty();
    }

    /**
     * 如果string为null，将string转为空串，否则保持string值不变.
     *
     * @param string
     * @return
     */
    public static String nullToEmpty(final String string) {
        return string == null ? "" : string;
    }

    /**
     * 字符串按指定长度右补0
     *
     * @param string 字符串
     * @param size   长度
     * @return String
     */
    public static String getFixedStr(final String string, final int size) {
        if (isNullOrEmpty(string)) {
            StringBuffer strBuffer = new StringBuffer("");
            for (int j = 0; j < size; j++) {
                strBuffer.append(" ");
            }
            return strBuffer.toString();
        } else {
            String str = string.trim();
            if (str.length() > size) {
                return str.substring(size);
            } else {
                StringBuffer stringBuffer = new StringBuffer(str);
                for (int i = 0; i < size - str.length(); i++) {
                    stringBuffer.append("0");
                }
                return stringBuffer.toString();
            }
        }
    }

    /**
     * 字符串按指定长度左补0
     *
     * @param string 字符串
     * @param size   长度
     * @return String
     */
    public static String getFixedNumber(final String string, final int size) {
        if (isNullOrEmpty(string)) {
            StringBuffer strBuffer = new StringBuffer("");
            for (int j = 0; j < size; j++) {
                strBuffer.insert(0, "0");
            }
            return strBuffer.toString();
        } else {
            String str = string.trim();
            if (str.length() > size) {
                return str.substring(size);
            } else {
                StringBuffer stringBuffer = new StringBuffer(str);
                for (int i = 0; i < size - str.length(); i++) {
                    stringBuffer.insert(0, "0");
                }
                return stringBuffer.toString();
            }
        }
    }

    /**
     * 返回指定长度的字符串
     * @param string
     * @param size
     * @return
     */
    public static String getFixedSize(final String string, final int size) {
        if (isNullOrEmpty(string)) {
            StringBuffer strBuffer = new StringBuffer("");
            for (int j = 0; j < size; j++) {
                strBuffer.insert(0, "0");
            }
            return strBuffer.toString();
        } else {
            String str = string.trim();
            if (str.length() > size) {
                return str.substring(str.length()-size,str.length());
            } else {
                StringBuffer stringBuffer = new StringBuffer(str);
                for (int i = 0; i < size - str.length(); i++) {
                    stringBuffer.insert(0, "0");
                }
                return stringBuffer.toString();
            }
        }
    }

    public static final String getStrFromInputSteam(final InputStream inputStream) throws IOException {
        final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("GBK")));
        final StringBuffer buffer = new StringBuffer();
        try {
            String line = "";
            while ((line = bufferedReader.readLine()) != null) {
                buffer.append(line);
            }
        } finally {
            inputStream.close();
        }
        return buffer.toString();
    }


    /**
     * 字符串按指定长度右补空格
     *
     * @param string 字符串
     * @param size   长度
     * @return String
     */
    public static String getFixedString(final String string, final int size) {
        if (isNullOrEmpty(string)) {

            StringBuffer strBuffer = new StringBuffer("");
            for (int j = 0; j < size; j++) {
                strBuffer.append(" ");
            }
            return strBuffer.toString();
        } else {
            StringBuffer strBuffer = new StringBuffer(string);
            for (int j = 0; j < size - string.length(); j++) {
                strBuffer.append(" ");
            }
            return strBuffer.toString();

        }
    }

    /**
     * 左去0，右补俩0
     *
     * @param pdAmt 金额1
     * @param Idec  调整后金额小数位数
     * @return
     */
    public static String DealMoneyStr(final String pdAmt, final int Idec) {

        if (isNullOrEmpty(pdAmt)) {
            StringBuffer strBuffer = new StringBuffer("");
            for (int i = 0; i < Idec; i++) {
                strBuffer.append("0");
            }
            return strBuffer.toString();
        } else {
            String s = pdAmt.replaceAll("^(0+)", "");
            StringBuffer strBuffer = new StringBuffer(s);
            for (int i = 0; i < Idec; i++) {
                strBuffer.append("0");
            }
            return strBuffer.toString();
        }

    }


    /**
     * 替换掉字符串中的某几个字符  从位置n到位置i之间的字符替换（包左不包右）
     * @param oldString
     * @param n
     * @return
     */
    public static String getReplaceString(final String oldString,final String replaceString,final int n,final int i){
        try {
            StringBuilder buil = new StringBuilder(oldString);
            StringBuilder replace = buil.replace(n, i, replaceString);
            return replace.toString();
        }catch (Exception e){
            return null;
        }
    }
}
